import React from 'react';
import {Spin, Card, Button, Table} from 'antd';
import DescriptionList from 'ant-design-pro/lib/DescriptionList';
import PropTypes from 'prop-types';
import {originTag, faultHisType, partClaimRepairHisType, authStatusHis} from '../../Enum';
import routes from '../routes';
import {DATE_FORMAT, TABLE} from '../../constants';
import {conventEnumValueToString, formatDateTime, formatAmount, formatPercent, onDownloadHistory} from '../../utils';
import {getHistoryDetail, getHistoryDownload} from '../api';
import {injectIntl} from '../intl';

const {Description} = DescriptionList;

class HistoryDetailPanel extends React.PureComponent {
    state = {
        isFetching: false,
        data: {},
        subClaimApplyMaterialDetailHis: [],
        subClaimApplyProcessDetailHis: [],
    }
    componentDidMount() {
        this.getDetailInfo();
    }
    getDetailInfo = () => {
        const id = this.props.id;
        this.setState({isFetching: true});
        getHistoryDetail(id)
            .then(res => {
                if(res.ok)
                    this.setState({
                        isFetching: false,
                        data: res.data,
                        subClaimApplyMaterialDetailHis: res.data.subClaimApplyMaterialDetailHis,
                        subClaimApplyProcessDetailHis: res.data.subClaimApplyProcessDetailHis,
                    });
                else
                    this.setState(({
                        isFetching: false
                    }));
            });
    }

    onCancel = () => {
        this.props.history.push(routes.historyQuery.url());
    }

    onClickDownload = e => {
        const {id, applyid} = e.target.dataset;
        this.setState({
            isFetching: true
        });
        onDownloadHistory({
            filePath: id,
            fileName: applyid
        });
    }
    render() {
        const {data, subClaimApplyMaterialDetailHis, subClaimApplyProcessDetailHis} = this.state;
        const {attachments = []} = data;
        const {formatMessage} = this.props.intl;
        const subClaimApplyMaterialDetailHisColumn = [{
            title: formatMessage({
                id: 'historyDetailPanel.column.usedPartCode',
                defaultMessage: '备件编号'
            }),
            dataIndex: 'usedPartCode',
        }, {
            title: formatMessage({
                id: 'historyDetailPanel.column.usedPartName',
                defaultMessage: '备件名称'
            }),
            dataIndex: 'usedPartName',
        }, {
            title: formatMessage({
                id: 'historyDetailPanel.column.isDc',
                defaultMessage: '是否地产化的'
            }),
            dataIndex: 'isDc',
            render: text => text ? formatMessage({
                id: 'historyDetailPanel.column.yes',
                defaultMessage: '是'
            }) : formatMessage({
                id: 'historyDetailPanel.column.no',
                defaultMessage: '否'
            })
        }, {
            title: formatMessage({
                id: 'historyDetailPanel.column.isCaused',
                defaultMessage: '是否主因件'
            }),
            dataIndex: 'isCaused',
            render: text => text ? formatMessage({
                id: 'historyDetailPanel.column.yes',
                defaultMessage: '是'
            }) : formatMessage({
                id: 'historyDetailPanel.column.no',
                defaultMessage: '否'
            })
        }, {
            title: formatMessage({
                id: 'historyDetailPanel.column.quantity',
                defaultMessage: '数量'
            }),
            dataIndex: 'quantity'
        }, {
            title: formatMessage({
                id: 'historyDetailPanel.column.claimMaterialDetailPrice',
                defaultMessage: '单价'
            }),
            dataIndex: 'price',
            render: text => formatAmount(text)
        }, {
            title: formatMessage({
                id: 'historyDetailPanel.column.sumTotal',
                defaultMessage: '金额'
            }),
            dataIndex: 'sumTotal',
            render: text => formatAmount(text)
        }, {
            title: formatMessage({
                id: 'historyDetailPanel.column.mngRate',
                defaultMessage: '管理费率'
            }),
            dataIndex: 'mngRate',
            render: text => formatPercent(text)
        }, {
            title: formatMessage({
                id: 'historyDetailPanel.column.manageFee',
                defaultMessage: '管理费'
            }),
            dataIndex: 'manageFee',
            render: text => formatAmount(text)
        }];
        const subClaimApplyProcessDetailHisColumn = [
            {
                title: formatMessage({
                    id: 'historyDetailPanel.column.status',
                    defaultMessage: '处理状态'
                }),
                dataIndex: 'status',
                render: text => conventEnumValueToString(authStatusHis, text)
            },
            {
                title: formatMessage({
                    id: 'historyDetailPanel.column.operateDate',
                    defaultMessage: '处理日期'
                }),
                dataIndex: 'operateDate',
                render: text => formatDateTime(text, DATE_FORMAT)
            },
            {
                title: formatMessage({
                    id: 'historyDetailPanel.column.handlerName',
                    defaultMessage: '经手人'
                }),
                dataIndex: 'handlerName',
            },
            {
                title: formatMessage({
                    id: 'historyDetailPanel.column.remark',
                    defaultMessage: '处理意见'
                }),
                dataIndex: 'remark',
            },
        ];
        return (
            <div>
                <Spin spinning={this.state.isFetching}>
                    <Card>
                        <DescriptionList size="large" title={formatMessage({
                            id: 'historyDetailPanel.card.subClaimApplyHistory',
                            defaultMessage: '二级服务站索赔预申请历史详情'
                        })}>
                            {this.props.isDealerUser &&
                            <Description term={formatMessage({
                                id: 'historyDetailPanel.operation.subDealerCode',
                                defaultMessage: '经销商编号'
                            })}>{data.subDealerCode}
                            </Description>
                            }
                            {this.props.isDealerUser &&
                            <Description term={formatMessage({
                                id: 'historyDetailPanel.operation.subDealerShortName',
                                defaultMessage: '经销商名称'
                            })}>{data.subDealerShortName}
                            </Description>
                            }
                            <Description term={formatMessage({
                                id: 'historyDetailPanel.operation.code',
                                defaultMessage: '预申请单号'
                            })}>{data.code}</Description>
                            <Description term={formatMessage({
                                id: 'historyDetailPanel.operation.createTime',
                                defaultMessage: '上报日期'
                            })}>{formatDateTime(data.createTime, DATE_FORMAT)}</Description>
                            <Description term={formatMessage({
                                id: 'historyDetailPanel.operation.originTag',
                                defaultMessage: 'CBU/KD'
                            })}>{conventEnumValueToString(originTag, data.originTag)}</Description>
                            <Description term={formatMessage({
                                id: 'historyDetailPanel.operation.authType',
                                defaultMessage: '授权方式'
                            })}>{conventEnumValueToString(partClaimRepairHisType, data.authType)}</Description>
                            <Description term={formatMessage({
                                id: 'historyDetailPanel.operation.faultType',
                                defaultMessage: '故障类型'
                            })}>{conventEnumValueToString(faultHisType, data.faultType)}</Description>
                            <Description term={formatMessage({
                                id: 'historyDetailPanel.operation.countryCode',
                                defaultMessage: '国家代码'
                            })}>{data.countryCode}</Description>
                            <Description term={formatMessage({
                                id: 'historyDetailPanel.operation.vin',
                                defaultMessage: 'VIN'
                            })}>{data.vin}</Description>
                            <Description term={formatMessage({
                                id: 'historyDetailPanel.operation.engineCode',
                                defaultMessage: '发动机号'
                            })}>{data.engineCode}</Description>
                            <Description term={formatMessage({
                                id: 'historyDetailPanel.operation.transmissionSn',
                                defaultMessage: '变速箱号'
                            })}>{data.transmissionSn}</Description>
                            <Description term={formatMessage({
                                id: 'historyDetailPanel.operation.purchaseDate',
                                defaultMessage: '购车日期'
                            })}>{formatDateTime(data.purchaseDate, DATE_FORMAT)}</Description>
                            <Description term={formatMessage({
                                id: 'historyDetailPanel.operation.mileage',
                                defaultMessage: '行驶里程'
                            })}>{data.mileage}</Description>
                            <Description term={formatMessage({
                                id: 'historyDetailPanel.operation.brand',
                                defaultMessage: '品牌'
                            })}>{data.brand}</Description>
                            <Description term={formatMessage({
                                id: 'historyDetailPanel.operation.series',
                                defaultMessage: '车系'
                            })}>{data.series}</Description>
                            <Description term={formatMessage({
                                id: 'historyDetailPanel.operation.model',
                                defaultMessage: '车型'
                            })}>{data.model}</Description>
                            <Description term={formatMessage({
                                id: 'historyDetailPanel.operation.customerCompany',
                                defaultMessage: '客户公司'
                            })}>{data.customerCompany}</Description>
                            <Description term={formatMessage({
                                id: 'historyDetailPanel.operation.customerName',
                                defaultMessage: '客户名称'
                            })}>{data.customerName}</Description>
                            <Description term={formatMessage({
                                id: 'historyDetailPanel.operation.contactPhone',
                                defaultMessage: '联系电话'
                            })}>{data.contactPhone}</Description>
                            <Description term={formatMessage({
                                id: 'historyDetailPanel.operation.address',
                                defaultMessage: '联系地址'
                            })}>{data.address}</Description>
                            <Description term={formatMessage({
                                id: 'historyDetailPanel.operation.email',
                                defaultMessage: '邮箱'
                            })}>{data.email}</Description>
                            <Description term={formatMessage({
                                id: 'historyDetailPanel.operation.laborFee',
                                defaultMessage: '工时金额小计'
                            })}>{formatAmount(data.laborFee)}</Description>
                            <Description term={formatMessage({
                                id: 'historyDetailPanel.operation.faultDescription',
                                defaultMessage: '故障描述'
                            })}>{data.faultDescription}</Description>
                            <Description term={formatMessage({
                                id: 'historyDetailPanel.operation.analysisResult',
                                defaultMessage: '检查分析结果'
                            })}>{data.analysisResult}</Description>
                            <Description term={formatMessage({
                                id: 'historyDetailPanel.operation.appraiser',
                                defaultMessage: '技术分析员'
                            })}>{data.appraiser}</Description>
                            <Description term={formatMessage({
                                id: 'historyDetailPanel.operation.analysisDate',
                                defaultMessage: '检查日期'
                            })}>{formatDateTime(data.analysisDate, DATE_FORMAT)}</Description>
                            <Description term={formatMessage({
                                id: 'historyDetailPanel.operation.proposedDisposalAdvice',
                                defaultMessage: 'Dealer处理意见'
                            })}>{data.proposedDisposalAdvice}</Description>
                            <Description term={formatMessage({
                                id: 'historyDetailPanel.operation.authCode',
                                defaultMessage: '授权码'
                            })}>{data.authCode}</Description>
                        </DescriptionList>
                    </Card>
                    <Card>
                        <DescriptionList size="large" title={formatMessage({
                            id: 'historyDetailPanel.table.subClaimApplyMaterialDetailHis',
                            defaultMessage: '索赔预申请材料清单'
                        })}>
                            <Table
                                className="white-space-nowrap"
                                columns={subClaimApplyMaterialDetailHisColumn}
                                rowKey="id"
                                pagination={false}
                                dataSource={subClaimApplyMaterialDetailHis}
                                {...TABLE} />
                        </DescriptionList>
                    </Card>
                    <Card>
                        <DescriptionList size="large" title={formatMessage({
                            id: 'historyDetailPanel.table.subClaimApplyProcessDetailHis',
                            defaultMessage: '索赔预申请操作历史清单'
                        })}>
                            <Table
                                className="white-space-nowrap"
                                columns={subClaimApplyProcessDetailHisColumn}
                                rowKey="id"
                                pagination={false}
                                dataSource={subClaimApplyProcessDetailHis}
                                {...TABLE} />
                        </DescriptionList>
                    </Card>
                    {attachments && attachments.length >= 0
                        ? <Card><DescriptionList size="large" title={formatMessage({
                            id: 'historyDetailPanel.card.attachment',
                            defaultMessage: '历史附件'
                        })}>
                            {attachments.map(item => (<Description key={item.id}>
                                <a onClick={this.onClickDownload} key="delete" data-id={item.filePath} data-applyid={item.fileName}>
                                    {item.fileName}</a></Description>))}
                        </DescriptionList></Card>
                        : <Card><DescriptionList size="large" title={formatMessage({
                            id: 'historyDetailPanel.content.noAttachment',
                            defaultMessage: '暂无附件'
                        })}>
                        </DescriptionList></Card>}
                </Spin>
                <div className="page-toolbar">
                    <Button type="primary" onClick={this.onCancel}>{formatMessage({
                        id: 'historyDetailPanel.operation.onCancel',
                        defaultMessage: '返回'
                    })}</Button>
                </div>
            </div>
        );
    }
}

HistoryDetailPanel.propTypes = {
    history: PropTypes.object,
    id: PropTypes.string,
    intl: PropTypes.object
};
import {connect} from 'react-redux';
import selectorFactory from 'Shared/utils/immutableToJsSelectorFactory';

const getDealerUser = selectorFactory(['page', 'domain', 'initData', 'isDealerUser']);

const mapStateToProps = state => ({
    isDealerUser: getDealerUser(state),
});

export default connect(mapStateToProps, null)(injectIntl(HistoryDetailPanel));
